package com.siping.并发;

import com.alibaba.fastjson.JSONObject;

/**
 * @author YangXu
 * @date 2021/10/29
 * @description: 测试对json中value字符串进行synchronized
 */
public class SyncStringTest {

    public static void main(String[] args) {
        for (int i = 0; i < 50; i++) {
            System.out.println(i);
            new Thread(()->{
                String json1 = "{\"id\" : \"7006da31d68f4e2d8c19f95a2d613926\"}";
//                String id = "7006da31d68f4e2d8c19f95a2d613926";
                String id = JSONObject.parseObject(json1).getString("id");
                System.out.println(System.identityHashCode(id));
                synchronized (id) {
                    System.out.println(Thread.currentThread().getName() + "进来了...");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "出去了...");
                }
            }, "thread-" + i).start();
        }
    }
}
